﻿using System;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32;

namespace OpenNETCF
{
    public static class Scanner
    {
        static Scanner()
        {
            DeviceVendor = GetDeviceVendor();
        }

        private static DevicePlatform GetDeviceVendor()
        {
            // TODO: this entire method needs to be validated and improved.  
            //       At development time all I had was a Honeywell 6100 device, so this is all based on educated guessing.

            using (var key = Registry.LocalMachine.OpenSubKey("SOFTWARE"))
            {
                var names = key.GetSubKeyNames();

                foreach (var n in names)
                {
                    if (n == "Honeywell")
                    {
                        return DevicePlatform.Honeywell;
                    }
                }
            }

            using (var key = Registry.LocalMachine.OpenSubKey("Platform"))
            {
                var name = key.GetValue("Name", "unknown").ToString();

                if (name == "D7600 Platform")
                {
                    return DevicePlatform.Dolphin7600;
                }
            }

            return DevicePlatform.Unknown;
        }

        public static DevicePlatform DeviceVendor { get; private set; }

        public static CompatibleDecodeControl GetCompatibleDecodeControl()
        {
            CompatibleDecodeControl scanner;

            switch (DeviceVendor)
            {
                case DevicePlatform.HHP_PPC:
                    // scanner = new HHPPPCDecodeControl();
                    throw new PlatformNotSupportedException();
                case DevicePlatform.Dolphin7600:
                    scanner = new HHPDecodeAssembly();
                    break;
                case DevicePlatform.Honeywell:
                    scanner = new HoneywellDecodeControl();
                    break;
                default:
                    scanner = new SimulatedDecodeControl();
                    break;
            }            

            // set up the scanner global defaults
            scanner.AimerTimeout = 0;
            scanner.AimIDDisplay = false;
            scanner.AudioMode = AudioDevice.SND_STANDARD;
            scanner.AutoLEDs = true;
            scanner.AutoScan = true;
            scanner.AutoSounds = false;
            scanner.CodeIDDisplay = false;
            scanner.ContinuousScan = false;
            scanner.DecodeAttemptLimit = 800;
            scanner.DecodeMode = DecodeMode.DM_STANDARD;
            scanner.LightsMode = ScanLightsMode.LM_ILLUM_AIMER;
            scanner.LinearRange = 3;
            scanner.ModifierDisplay = false;
            scanner.PrintWeight = 4;
            scanner.ScanTimeout = 5000;
            scanner.SearchTimeLimit = 400;
            scanner.TraceMode = false;
            scanner.TriggerKey = TriggerKeyEnum.TK_ONSCAN;
            scanner.VideoReverse = false;
            scanner.VirtualKeyMode = false;
            scanner.VirtualKeyTerm = VirtualKeyTermEnum.VK_NONE;
            scanner.Visible = false;

            return scanner;
        }

        public static CompatibleSymbologyConfig GetCompatibleSymbologyConfig(SymID symId)
        {
            switch (DeviceVendor)
            {
                case DevicePlatform.HHP_PPC:
                    throw new NotSupportedException("Pocket PC not currently supported");
//                    return new HHPPPCSymbologyConfig(symId);
                case DevicePlatform.Dolphin7600:
                    return new HHPSymbologyConfig(symId);
                case DevicePlatform.Honeywell:
                    return new HoneywellSymbologyConfig(symId);
                default:
                    return new SimulatedSymbologyConfig();
            }            
        }

        public static string GetDeviceInfo(uint code)
        {
            switch (DeviceVendor)
            {
                case DevicePlatform.HHP_PPC:
                    return "PocketPC";
                case DevicePlatform.Dolphin7600:
                    return HHP.PDTDevice.UtilMethods.GetDeviceInfo(code);
                case DevicePlatform.Honeywell:
                    return Honeywell.PDTDevice.UtilMethods.GetDeviceInfo(code);
                default:
                    return "Emulator";
            }
        }

        public static string GetSerialNumber()
        {
            switch (DeviceVendor)
            {
                case DevicePlatform.Dolphin7600:
                    return HHP.PDTDevice.UtilMethods.GetSerialNumber();
                case DevicePlatform.Honeywell:
                    return Honeywell.PDTDevice.UtilMethods.GetSerialNumber();
                default:
                    return "EMULATOR";
            }
        }

        public static void ColdReboot()
        {
            switch (DeviceVendor)
            {
                case DevicePlatform.Dolphin7600:
                    HHP.PDTDevice.UtilMethods.ColdReboot();
                    break;
                case DevicePlatform.Honeywell:
                    Honeywell.PDTDevice.UtilMethods.ColdReboot();
                    break;
                default:
                    throw new PlatformNotSupportedException();
            }
        }
    }
}
